<template>
  <data-table-app-page>
    <div slot="toolbar">
      <el-form inline :model="searchForm">
        <el-form-item label="订单号">
          <el-input v-model="searchForm.orderno" clearable style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="订单状态">
          <type-select
            v-model="searchForm.status"
            :items="DictMan.items('productOrder.status')"
            style="width: 110px"
          />
        </el-form-item>
        <el-form-item label="支付状态">
          <type-select
            v-model="searchForm.payStatus"
            :items="DictMan.items('payStatus')"
            style="width: 110px"
          />
        </el-form-item>
        <el-form-item label="支付方式">
          <type-select
            v-model="searchForm.payType"
            :items="DictMan.items('payType')"
            style="width: 110px"
          />
        </el-form-item>
        <el-form-item label="下单人">
          <el-input
            v-model="creatorName"
            clearable
            style="width: 100px"
            @focus="onCreatorNameInputFocus"
            @clear="creatorName = '', searchForm.creator = null"></el-input>
        </el-form-item>
        <el-form-item label="服务人员">
          <el-input
            v-model="serviceStaff"
            clearable
            style="width: 120px"
            @focus="onServiceStaffInputFocus"
            @clear="serviceStaff = '', searchForm.handler = null"></el-input>
        </el-form-item>
        <el-form-item label="下单日期">
          <el-date-picker
            v-model="searchForm.startCreateTime"
            value-format="yyyy-MM-dd"
            align="right"
            type="date"
            placeholder="选择日期"
            style="width: 140px">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <data-table-query-button
            :query-params="() => ({
              ...searchForm,
              ...(searchForm.startCreateTime ? {
                  startCreateTime: searchForm.startCreateTime + ' 00:00:00',
                  endCreateTime: searchForm.startCreateTime + ' 23:59:59'
                } : {})
            })" />
        </el-form-item>
      </el-form>
      <el-button
        v-if="$auth('place') && this.$params.shop.industryId != 'activity'"
        type="primary"
        icon="el-icon-plus"
        @click="onAddClick"
      >
        下单
      </el-button>
      <el-button
        v-auth:export
        type="default"
        icon="el-icon-download"
        @click="onExportClick"
      >
        导出
      </el-button>
    </div>
    <data-table
      ref="table"
      url="/api/shop/order/orderPage"
      lazy
      timing
    >
      <el-table-column prop="orderno" label="订单号" width="190"></el-table-column>
      <el-table-column prop="status" label="订单状态" width="120" v-slot="{row}">
        <status-tag :status="row.status" />
      </el-table-column>
      <el-table-column prop="paymentFee" label="订单金额" width="90"></el-table-column>
      <el-table-column prop="payStatus" label="支付状态" width="90" v-slot="{row}">
        <pay-status-tag :status="row.payStatus" />
      </el-table-column>
      <el-table-column prop="payTypeText" label="支付方式" width="120"></el-table-column>
      <el-table-column prop="linkman" label="下单人" width="120"></el-table-column>
      <el-table-column prop="productNames" label="商品" width="120" show-overflow-tooltip></el-table-column>
      <el-table-column prop="serviceStaff" label="服务人员" width="120" show-overflow-tooltip></el-table-column>
      <el-table-column prop="remark" label="订单备注" width="120" show-overflow-tooltip></el-table-column>
      <el-table-column prop="duration" label="预估时长" width="120" :formatter="(r, c, v) => v + '分钟'"></el-table-column>
      <el-table-column prop="createTime" label="下单时间" width="152"></el-table-column>
      <el-table-column label="操作" fixed="right" min-width="150">
        <template slot-scope="scope">
          <el-button
            type="primary"
            plain
            @click="onDetailsClick(scope.row)">
            详情
          </el-button>
          <el-button
            v-if="
              $auth('taking')
              && ['housekeeping', 'catering'].includes(scope.row.industryId)
              && (scope.row.status == 10 || scope.row.status == 12)"
            type="primary"
            plain
            @click="onTakingClick(scope.row)"
          >
            {{$params.shop.businessMode == 0 ? '接单' : '派单'}}
          </el-button>
        </template>
      </el-table-column>
    </data-table>
    
    <employee-query-selector ref="employeeQuerySelector" />
    <user-query-selector ref="userQuerySelector" />
  </data-table-app-page>
</template>

<script>
import StatusTag from './StatusTag';
import PayStatusTag from './PayStatusTag';
import { stringify } from 'qs';

export default {
  pageProps: {
    title: '订单管理'
  },
  components: {
    StatusTag,
    PayStatusTag,
    EmployeeQuerySelector: () => import('../EmployeeQuerySelector.vue'),
    UserQuerySelector: () => import('@/pages/user/UserQuerySelector.vue')
  },
  data() {
    return {
      permission: '/pension_service_center/provider/shop/order',
      creatorName: null,
      serviceStaff: null,
      searchForm: {
        orderno: null,
        status: null,
        payStatus: null,
        payType: null,
        creator: null,
        handler: null,
        startCreateTime: null,
        providerId: this.$params.shop.providerId
      }
    }
  },
  methods: {
    onAddClick(order) {
      const { shop } = this.$params;
      if (['housekeeping', 'catering'].includes(shop.industryId)) {
        this.pushPage({
          path: '/shop/order/flow/place',
          params: {
            shop,
            orderTable: this.$refs.table
          },
          subTitle: shop.name
        });
      } else {
        this.openTab({
          url: "/shop/order/orderAdd.do?providerId=" + shop.providerId,
          title: shop.name + " - 下单"
        });
      }
    },
    onTakingClick(order) {
      const submit = async (handler) => {
        handler = handler || this.$params.shop.adminId;
        let operator = this.$params.shop.adminId;
        const ret = await this.$http.post(
          '/api/shop/order/taking',
          {
            orderno: order.orderno,
            creator: order.creator,
            operator,
            handler
          });
        if (ret.success) {
          order.status = 13;
          order.operator = operator;
          order.handler = handler;
          this.$message.success((this.$params.shop.businessMode == 0 ? '接单' : '派单') + '成功');
        } else {
          this.$message.error(ret.message);
        }
      };

      if (this.$params.shop.businessMode == 1) {
        this.$refs.employeeQuerySelector.show({
          title: '指派订单服务人员',
          queryParams: {
            providerId: order.providerId
          },
          onOk: (employee) => {
            submit(employee.adminId);
            return true;
          }
        });
      } else {
        submit();
      }
    },
    onCreatorNameInputFocus() {
      this.searchForm.creator = null;
      this.creatorName = null;
      this.$refs.userQuerySelector.show({
        onOk: (user) => {
          this.searchForm.creator = user.id;
          this.creatorName = user.realName || user.aliasName;
          return true;
        }
      });
    },
    onServiceStaffInputFocus() {
      this.searchForm.handler = null;
      this.serviceStaff = null;
      this.$refs.employeeQuerySelector.show({
        queryParams: {
          providerId: this.$params.shop.providerId
        },
        onOk: (employee) => {
          this.searchForm.handler = employee.adminId;
          this.serviceStaff = employee.realName;
          return true;
        }
      });
    },
    onDetailsClick(order) {
      this.pushPage({
        path: '/shop/order/details',
        params: {
          order,
          shop: this.$params.shop,
          onDeleteSuccess: () => {
            this.$refs.table.reloadCurrentPage();
          }
        },
        key: order.orderno,
      });
    },
    onExportClick() {
      this.$message.info('正在处理导出，请稍等...');
      location.href = "api/shop/order/export?" + stringify(this.searchForm);
    }
  },
  mounted() {
    this.$refs.table.query(this.searchForm);
  }
}
</script>